package com.google.jessewilson.codesearch.commandlinesearch;

import com.google.jessewilson.codesearch.api.SearchProvider;
import com.google.jessewilson.codesearch.api.spi.SearchProviderConfigurationEditor;
import com.google.jessewilson.codesearch.configuration.SearchProviderConfiguration;
import com.google.jessewilson.codesearch.ui.IntegerEditor;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;

/**
 * @author jessewilson
 */
class ClsConfigurationEditor implements SearchProviderConfigurationEditor {

  private final JTextField executableAndArguments;
  private final JTextField parseLineRegex;
  private final IntegerEditor filePathMatch;
  private final IntegerEditor lineNumberMatch;
  private final IntegerEditor lineValueMatch;

  private JPanel panel;

  public ClsConfigurationEditor() {
    executableAndArguments = new JTextField(5);
    parseLineRegex = new JTextField(5);

    filePathMatch = new IntegerEditor();
    lineNumberMatch = new IntegerEditor();
    lineValueMatch = new IntegerEditor();

    FormLayout formLayout = new FormLayout(
        "right:min, 3px, left:pref:grow",
        "min, 13px, min, 3px, min, 3px, min, 3px, min");
    panel = new JPanel(formLayout);

    CellConstraints cc = new CellConstraints();
    panel.add(new JLabel("Command:"), cc.xy(1, 1));
    panel.add(executableAndArguments, cc.xy(3, 1, "fill, default"));
    panel.add(new JLabel("Regular Expression:"), cc.xy(1, 3));
    panel.add(parseLineRegex, cc.xy(3, 3, "fill, default"));
    panel.add(new JLabel("File Path Match:"), cc.xy(1, 5));
    panel.add(filePathMatch.getComponent(), cc.xy(3, 5));
    panel.add(new JLabel("Line Number Match:"), cc.xy(1, 7));
    panel.add(lineNumberMatch.getComponent(), cc.xy(3, 7));
    panel.add(new JLabel("Line Value Match:"), cc.xy(1, 9));
    panel.add(lineValueMatch.getComponent(), cc.xy(3, 9));
  }

  public JPanel getComponent() {
    return panel;
  }

  public void setValue(SearchProviderConfiguration value) {
    assert(value != null);

    ClsConfiguration configuration = (ClsConfiguration) value;

    // setting the arguments requires concatenating
    String argumentsString = configuration.getArguments() != null ? configuration.getArguments() : "";
    String arguments = argumentsString.length() > 0
        ? " " + argumentsString
        : "";
    String executableString = configuration.getExecutable() != null ? configuration.getExecutable() : "";
    executableAndArguments.setText(executableString + arguments);

    String parseLineRegexString = configuration.getParseLineRegex() != null ? configuration.getParseLineRegex() : "";
    parseLineRegex.setText(parseLineRegexString);
    filePathMatch.setValue(configuration.getFilePathMatch());
    lineNumberMatch.setValue(configuration.getLineNumberMatch());
    lineValueMatch.setValue(configuration.getLineValueMatch());
  }

  public SearchProviderConfiguration getValue() {
    ClsConfiguration.Builder builder = new ClsConfiguration.Builder();

    // setting the executable and arguments requires parsing
    String executableAndArguments = this.executableAndArguments.getText();
    String[] executableAndArgumentsArray = executableAndArguments.split("\\s", 2);
    if(executableAndArgumentsArray.length >= 1) {
      builder.executable(executableAndArgumentsArray[0]);
    }
    if(executableAndArgumentsArray.length == 2) {
      builder.arguments(executableAndArgumentsArray[1]);
    }

    builder.parseLineRegex(parseLineRegex.getText());

    Integer filePathMatch = this.filePathMatch.getValue();
    if (filePathMatch != null) builder.filePathMatch(filePathMatch);
    Integer lineNumberMatch = this.lineNumberMatch.getValue();
    if (lineNumberMatch != null) builder.lineNumberMatch(lineNumberMatch);
    Integer lineValueMatch = this.lineValueMatch.getValue();
    if (lineValueMatch != null) builder.lineValueMatch(lineValueMatch);

    return builder.build();
  }

  public SearchProvider getSearchProvider() {
    return new ClsSearchProvider();
  }

  public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() { public void run() {
          JPanel panel = new ClsConfigurationEditor().panel;
          panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

          JFrame frame = new JFrame();
          frame.getContentPane().add(panel);
          frame.setSize(400, 200);
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
      }});
  }

}
